home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: freeing structures
- Date: 7 Feb 1996 17:35:01 GMT
- Organization: OpenVision
- Message-ID: <4fans5$htt@spanky.pls.ov.com>
- References: <4f7913$brg@sepia.nioz.nl>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article brg@sepia.nioz.nl, rikko@nioz.nl (Rikko Verrijzer) writes:
- >Hi,
- >
- >I have learnt C++ at my school past year, but now I have to build a programm in just
- >plain Standard C. the problem with this is, that several C types where new to me.
- >as example typedef.... in C++ I used a class to do this...
- >
- >/*
- > * Data structure used for storing the loaded file.
- > */
- >
- >typedef struct Line {
- >
- > struct Line *next; /* Pointer to next item */
- > char **dpart[3];
- > char **DateLine[3]; /* contains the date in form yy-mm-dd */
- > char *RestLine; /* list of the Values of the line */
- >} Line;
- >
- >In my programm I have to load a ascii file, my approch was to build a linked list
- >of struct line (which I defiened with typedef Line), a struct line will contain data
- >and a pointer to the next line. this part went fine but the problems starts with
- >clearing a file from memory, my approch to that was as follows. The errors created
- >by this function occor in two different ways, 1) they damage a line which was loaded
- >after the clearing 2) causes the programm to crash with a segmentation fault.
- >I know I have made a mistake in my memeroy administration but, I can't find the problem
- >
- >void ClearLines()
- >{
- >Line* line;
- >Line* line_next;
- >
- >
- > line=FirstLine;
- >
- > /*
- > * when no file is loaded the firstline will be NULL
- > */
- >
- > while(line!=(Line*)NULL && line->next!=(Line*)NULL)
- > {
- > /*
- > * ask for the next line while the currentline is still around
- > */
- >
- > line_next=line->next;
- >
- >
- [snipped a lot]
- > free(&line); /* I suspect this line to trigger the error */
- ^
- This is the problem. You must never give anything to free() that was not
- generated by malloc(). The above statement should be:
- free(line);
-
- Fletcher.Glenn@ov.com
-
- > line=line_next;
- > }
- >}
- >
- >
- >I hope some can find what I did wrong, I'm sure it is something simple but I can't
- >find it....
- >
- >Thanx for reading my question
- >
- >please email me if you have a solution... (rikko@nioz.nl)
- >
- > Greetings
- > Rikko
- >
- >
-
-
-
-
-
-